package com.itheima.web.controller;

import cn.hutool.core.util.StrUtil;
import com.itheima.web.manager.UserManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Struct;

@RestController
public class UserController {

    @Autowired
    private UserManager userManager;

    // 用户列表
    @GetMapping("/manage/users")
    public ResponseEntity users(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        // 调用manager
        return userManager.users(pageNum, pageSize);
    }

    // 用户详情
    @GetMapping("/manage/users/{userId}")
    public ResponseEntity findUserInfoById(@PathVariable Long userId) {
        // 调用manager
        return userManager.findUserInfoById(userId);
    }

    // 动态查询
    @GetMapping("/manage/messages")
    public ResponseEntity messages(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "uid", required = false) Long userId,
            String state) {
        // 处理动态状态
        Integer stateInt = null;
        if (!StrUtil.equals(state, "''") && StrUtil.isNotEmpty(state)) {
            stateInt = Integer.parseInt(state);
        }
        // 调用manager
        return userManager.messages(userId, stateInt, pageNum, pageSize);
    }

    // 动态详情
    @GetMapping("/manage/messages/{publishId}")
    public ResponseEntity findMovementVoById(@PathVariable String publishId){
        // 调用manager
        return userManager.findMovementVoById(publishId);
    }

    // 评论列表
    @GetMapping("/manage/messages/comments")
    public ResponseEntity findCommentVoByPage(
            @RequestParam(value = "page",defaultValue = "1")Integer pageNum,
            @RequestParam(value = "pagesize",defaultValue = "10") Integer pageSize,
            @RequestParam(value = "messageID")String publishId){
        // 调用manager
        return userManager.findCommentVoByPage(publishId,pageNum,pageSize);
    }

}
